package com.fjrcloud.utils;

import com.fjrcloud.exception.BusinessException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by shengjian.lin on 17-2-8.
 */
public class SerialNumberUtils {

    private static String startTime;
    private static Long currentNumber = 0L;

    private static String getDate() throws ParseException {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
        if (startTime == null){
            startTime = sdf.format(new Date());
        }
        Long st = sdf.parse(startTime).getTime();
        Long ct = System.currentTimeMillis();

        long poor=(ct-st)/(1000*3600*24);

        if (poor >= 1){
            startTime = sdf.format(new Date(System.currentTimeMillis()));
        }


        return startTime;
    }

    private static String getNumber() throws BusinessException {

        if(currentNumber>=999999)
            throw new BusinessException("流水号超出");
        currentNumber++;

        String current = currentNumber.toString();

        if (current.length() == 6)
            return current;

        String zero = "";

        for (int i = 0; i < (6-current.length()); i++) {
            zero+="0";
        }

        return zero+current;
    }

    public static String get() throws BusinessException, ParseException {
        return getDate()+getNumber();
    }
}
